package cate.game.role.res.guild.po;

import cate.common.table.d.GDGuild;
import cate.common.table.guild.GuildFuncRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.role.Role;

import java.util.LinkedList;
import java.util.List;

public class GuildFuncUtil {
    private GuildFuncUtil() {
    }

    public static GameResult<List<Object>> checkFunkOpen(int funcId, GuildPO guild) {
        GameResult<List<Object>> r = new GameResult<>();

        r.data = new LinkedList<>();
        if (guild == null) {
            r.data.add(0);
            return r.fail("公会不存在");
        }
        GuildFuncRow row = getRow(funcId, guild);
        //未配置默认通过
        if(row == null){
            return r.success();
        }

        if (row.level > guild.basic.level) {
            r.data.add(GDGuild.ErrorMsgId.GUILD_LEVEL);
            r.data.add(row.level);
            return r.fail("公会等级%d时开启");
        }
        return r.success();
    }


    public static GameResult<List<Object>> checkFunkOpen(int funcId, GuildPO guild, Role role) {
        if (role == null) {
            return checkFunkOpen(funcId, guild);
        }
        GameResult<List<Object>> r = new GameResult<>();
        r.data = new LinkedList<>();
        GuildFuncRow row = getRow(funcId, guild);
        r = checkFunkOpen(funcId, guild);
        if (!r.ok()) {
            return r;
        }

        if (row != null && row.joinDay > TimeTool.getDistanceDays(role.getRes().guild.joinTime, System.currentTimeMillis())) {
            r.data.add(GDGuild.ErrorMsgId.GUILD_ENTER_DAY);
            r.data.add(row.joinDay);
            return r.fail("加入公会未满%d天");
        }

        return r.success();
    }

    public static GuildFuncRow getRow(int funcId, GuildPO guild) {
        return guild.game.table.guild.func.get(funcId);
    }
}
